package com.eduService.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eduService.teacher.entity.EduChapter;
import com.eduService.teacher.entity.EduVideo;
import com.eduService.teacher.mapper.EduChapterMapper;
import com.eduService.teacher.service.IEduChapterService;
import com.eduService.teacher.service.IEduVideoService;
import com.eduService.teacher.vo.chapter.chapterAndVideo;
import com.eduService.teacher.vo.chapter.video;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-05-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements IEduChapterService {

    @Autowired
    IEduVideoService iEduVideoService;

    /*
     *
     * 根据课程id获取章节和小节
     * */
    @Override
    public List<chapterAndVideo> getChapterAndVideo(String courseid) throws BeansException {
        QueryWrapper<EduChapter> queryWrapperChapter = new QueryWrapper<EduChapter>();
        queryWrapperChapter.eq("course_id", courseid);
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapperChapter);
        List<chapterAndVideo> chapterAndVideos = new ArrayList<>();
        QueryWrapper<EduVideo> queryWrapperVideo = new QueryWrapper<EduVideo>();
        queryWrapperVideo.eq("course_id", courseid);
        List<EduVideo> videoList = iEduVideoService.list(queryWrapperVideo);
        for (EduChapter e : eduChapters) {
            chapterAndVideo c = new chapterAndVideo();
            BeanUtils.copyProperties(e, c);
            String chapterid = e.getId();
            for (EduVideo video : videoList) {
                video v = new video();
                BeanUtils.copyProperties(video, v);
                if (video.getChapterId().equals(chapterid)) {
                    c.getVideo().add(v);
                    System.out.println(c);
                }
            }

            chapterAndVideos.add(c);
        }
        return chapterAndVideos;
    }

    /*
    * 判断该章节是否有小节，有则返回true ，没有则返回false
    * */
    @Override
    public boolean delChapter(String chapterid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id", chapterid);
        long count = iEduVideoService.count(wrapper);
        System.out.println(count);
        return count > 0;
    }
}
